package com.uday.kraterchat.chatclient.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.uday.kraterchat.chatclient.business.UserServiceDelegate;
	import com.uday.kraterchat.chatclient.control.KeepAliveEvent;
	import com.uday.kraterchat.chatclient.control.LogoutUserEvent;
	import com.uday.kraterchat.chatclient.model.ChatClientModel;
	
	import flash.display.DisplayObject;
	
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.events.CloseEvent;
	import mx.rpc.IResponder;

	public class KeepAliveCommand implements ICommand, IResponder
	{
		private var objChatClientModel:ChatClientModel = ChatClientModel.getInstance();
		
		public function KeepAliveCommand()
		{
		}

		public function execute(event:CairngormEvent):void
		{
			var objLoginUserDel:UserServiceDelegate = new UserServiceDelegate(this);
			objLoginUserDel.keepAlive((event as KeepAliveEvent).objUserInfoBaseItem);
			//objChatClientModel.strRecvdMessages += "\nAlive sgnl sent at " + new Date().toString();
		}
		
		public function result(data:Object):void
		{
			//objChatClientModel.strRecvdMessages += "\nAlive sgnl resp recvd at " + new Date().toString();
		}
		
		public function fault(info:Object):void
		{
			objChatClientModel.objKeepAliveTimer.stop();
			Alert.show("Server closed unexpectedly. Logging out.","Error",4,null,alertCloseHandler);
		}
		
		private function alertCloseHandler(event:CloseEvent):void {
			CairngormEventDispatcher.getInstance().dispatchEvent(new LogoutUserEvent(objChatClientModel.objCurrUser));
		}		
	}
}